<?php
namespace App\Admin\Controllers;
use App\Admin\Models\Notices;
use App\Admin\Models\SourceMaterial;
use Common\ControllerAdBase;
use Phalcon\Paginator\Adapter\Model as PaginatorModel;

class NoticesController extends ControllerAdBase
{

    public function initialize()
    {
        parent::initialize();
    }

    public function listAction()
    {
        //当前页数
        if (!isset($this->params['page']) || empty($this->params['page'])) {
            $page = 1;
        } else {
            $page = (int)$this->params['page'];
        }
        //当前条数
        if (!isset($this->params['limit']) || empty($this->params['limit'])) {
            $pageSize = 10;
        } else {
            $pageSize = (int)$this->params['limit'];
        }
        $notices    = new Notices();
        $list       = $notices->notices_list($this->params);
        $paginator  = new PaginatorModel(
            array(
                "data"  => $list,
                "limit" => $pageSize,
                "page"  => $page
            )
        );
        $list_items = $paginator->getPaginate();
        $this->ok($list_items);
    }

    /**
     * 获取公告性情
     */
    public function noticesdetailAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '公告详情id不能为空');
        }
        $notice = new Notices();
        $detail = $notice->notices_detail($this->params['id']);
        $detail['plat_name'] = '';
        if(!empty($detail['ids'])){
            $source = new SourceMaterial();
            $sourceInfo = $source->find_by_id($detail['ids']);
            if($sourceInfo){
                $detail['plat_name'] = $sourceInfo['name'];
            }
        }
        if ($detail) {
            $this->ok($detail);
        } else {
            $this->fail(-1, '该公告不存在或已删除');
        }
    }

    public function addnoticleAction()
    {
        $this->validata_filed($this->params);
        $notices_detail['title']      = $this->params['title'];
        $notices_detail['abstract']   = $this->params['abstract'];
        $notices_detail['content']    = $this->params['content'];
        $notices_detail['filepath']   = $this->params['filepath'];
        $notices_detail['published']  = $this->params['published'];
        $notices_detail['type']       = $this->params['type'];
        $notices_detail['created_at'] = date('Y-m-d H:i:s', time());
        $notices_detail['updated_at'] = date('Y-m-d H:i:s', time());
        $notices_detail['ids'] = $this->params['ids'];
        $notices_detail['publish_at'] = isset($this->params['publish_at']) && !empty($this->params['publish_at']) ? $this->params['publish_at'] : date('Y-m-d H:i:s');

        $router_arr = [];
        if($this->params['ids']){
            $sourceDb = new SourceMaterial();
            $source_find_one = $sourceDb->find_by_id_obj($this->params['ids']);
            if($source_find_one){
                $router_arr = $source_find_one->toarray();
                if($router_arr){
                    if($router_arr['type'] == 3){
                        $this->fail(-1,'不支持打开外部路由形式');
                    }
                }
            }
        }

        if(isset($this->params['publish_at']) && empty($this->params['publish_at'])){
            $notices_detail['is_push'] = 1;
        }

        $notice                       = new Notices();
        $result                       = $notice->notices_save($notices_detail);
        if($result){

            if(isset($this->params['publish_at']) && empty($this->params['publish_at'])){
                if($this->params['type'] != 1){
                    $router_arr['type'] = "2";
                    $router_arr['app_path'] = "H5UserId";
                    $router_arr['title'] = $this->params['type'] == -1 ? '平台公告' : '推荐活动';
                    $router_arr['url'] = "https://api.wenweiw.com/invite/noticedetail?id={$result->id}";
                    $router_arr['is_need_login'] = "1";
                }
                $this->jpush->pushmsg_all([],$this->params['title'],$this->params['abstract'],$router_arr);
            }

            $this->ok([]);
        }else{
            $this->fail(-1,'添加失败');
        }

    }


    public function updatenoticleAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '公告详情id不能为空');
        }
        $this->validata_filed($this->params);
        $notice                       = new Notices();
        $notices_detail               = $notice->notices_detail($this->params['id']);
        $notices_detail['title']      = $this->params['title'];
        $notices_detail['abstract']   = $this->params['abstract'];
        $notices_detail['content']    = $this->params['content'];
        $notices_detail['filepath']   = $this->params['filepath'];
        $notices_detail['published']  = $this->params['published'];
        $notices_detail['updated_at'] = date('Y-m-d H:i:s', time());
        $notices_detail['ids']        = $this->params['ids'];
        $result                       = $notice->notices_save($notices_detail);
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '修改失败');
        }
    }


    private function validata_filed($params)
    {
        if (empty($params['title'])) {
            $this->fail(-1, '标题不能为空');
        }
        if (empty($params['abstract'])) {
            $this->fail(-1, '简介不能为空');
        }
        if ($params['type'] == -1 || $params['type'] == 2) {
            if (empty($params['content'])) {
                $this->fail(-1, '内容不能为空');
            }
        }
    }


    public function delnoticleAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '公告详情id不能为空');
        }
        $notices        = new Notices();
        $notices_detail = $notices->findFirstById($this->params['id']);
        if (empty($notices_detail)) {
            $this->fail(-1, '文章不存在，或已被删除');
        }
        $result = $notices_detail->delete();
        if ($result) {
            $this->ok([]);
        } else {
            $this->fail(-1, '删除失败');
        }
    }

    public function catelistAction()
    {
        $position                 = isset($this->params['position']) ? $this->params['position'] : 0;
        $conditions['conditions'] = '';
        if ($position > 0) {
            $conditions['conditions'] = "position = $position";
        }
        $db   = new NoticesCate();
        $data = $db->find($conditions);
        $this->ok($data->toarray());
    }

    public function cateeditAction()
    {
        if (!isset($this->params['name']) || empty($this->params['name'])) {
            $this->fail(-1, '缺少参数name');
        }
        if (!isset($this->params['icon']) || empty($this->params['icon'])) {
            $this->fail(-1, '缺少参数icon');
        }
        $id = isset($this->params['id']) ? $this->params['id'] : 0;

        $data['position'] = $this->params['position'] ? $this->params['position'] : 2;
        $data['name']     = $this->params['name'];
        $data['icon']     = $this->params['icon'];
        $db               = new NoticesCate();
        if ($id > 0) {
            $info = $db->findFirst($id)->update($data);
            if ($info) {
                $this->ok('操作成功!');
            }
        } else {
            $data['created_at'] = date('Y-m-d H:i:s');
            if ($db->save($data)) {
                $this->ok('操作成功!');
            }
        }
        $this->fail(-1, $db->getmessage());
    }

    public function catedelAction()
    {
        if (!isset($this->params['id']) || empty($this->params['id'])) {
            $this->fail(-1, '缺少参数id');
        }
        $db   = new NoticesCate();
        $info = $db->findFirst($this->params['id'])->delete();
        if ($info) {
            $this->ok('操作成功!');
        }
        $this->fail(-1, $db->getmessage());
    }
}
